﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.DataAccessLayer;
using IQKS.AWP.BusinessObject;
using IQKS.AWP.BusinessAccessLayer;
using System.Globalization;
using System.Configuration;
public partial class Admin_AssessorInvoice : System.Web.UI.Page
{
    public string DocumentPath = ConfigurationSettings.AppSettings["InvoiceDocumentUploadPath"];
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();

    private DataSet GetDataSet()
    {
        DataSet objDs = null;
        try
        {
            //objDs = new AssessorInvoiceBAL().GetAssessorInvoice();
            objDs = new AssessorInvoiceBAL().GetCandidateByAssessor(CommonUtility.GetLoggedOnUserId());
            //LalblUploadInvoice.Text = "* Acceptable file formats " + Convert.ToString(ConfigurationManager.AppSettings["ReviewFileType"]).Replace("|", ",") + Convert.ToString(ConfigurationManager.AppSettings["FileSize"]);
            if (objDs != null)
            {
                if (objDs.Tables[0].Rows.Count > 0)
                {
                    if (objDs.Tables[0].Rows.Count < 7)
                    {
                        // dvGrid.Style.Add("height", "240px");
                    }
                    else
                    {
                        // dvGrid.Style.Add("height", "320px");
                    }
                }
            }
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        txtInvoiceDate.Attributes.Add("readonly", "readonly");
        txtInvoiceAmount.Attributes.Add("readonly", "readonly");
        return objDs;


    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
            //Session["UserID"] = Guid.NewGuid();
        }
    }
    private void ClearData()
    {
        txtInvoiceNo.Text = string.Empty;
        txtInvoiceDate.Text = string.Empty;
        txtInvoiceAmount.Text = string.Empty;
        txtInvoiceComments.Text = string.Empty;
        lblMessage.Text = string.Empty;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            AssessorInvoiceBAL objAIMBal = new AssessorInvoiceBAL();
            AssessorInvoiceBO objAIMBO = new AssessorInvoiceBO();
            //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
            string strReturnValue;

            //if (txtUploadInvoice.HasFile)
            //{
            //    //TODO Add As per physical file
            //    //create the path to save the file to
            //    string fileName = Path.Combine(Server.MapPath(DocumentPath), txtUploadInvoice.FileName);
            //    //save the file to our local path
            //    txtUploadInvoice.SaveAs(fileName);
            //    InvoiceFileSave();
            //}


            objAIMBO.SetMethod = "INSERT";
            objAIMBO.InvoiceID = 0;
            objAIMBO.InvoiceNo = txtInvoiceNo.Text.Trim();

            string[] date = new string[3];
            date[0] = txtInvoiceDate.Text.Trim().Substring(0, 2);
            date[1] = txtInvoiceDate.Text.Trim().Substring(3, 2);
            date[2] = txtInvoiceDate.Text.Trim().Substring(6, 4);

            objAIMBO.InvoiceDate = Convert.ToDateTime(date[2] + "/" + date[1] + "/" + date[0]);


            objAIMBO.InvoiceAmount = Convert.ToDouble(txtInvoiceAmount.Text.Trim());
            objAIMBO.InvoicePath = string.Empty; //txtUploadInvoice.FileName.Trim();
            objAIMBO.InvoiceComment = txtInvoiceComments.Text.Trim();
            objAIMBO.CreatedBy = CommonUtility.GetLoggedOnUserId();

            string strCandidatIDList = "";
            foreach (GridViewRow row in gvAInvoice.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkApproved = (CheckBox)(row.Cells[0].FindControl("chkApproved"));
                    if (chkApproved.Checked)
                    {
                        int CandidateID = Convert.ToInt32(gvAInvoice.DataKeys[row.RowIndex].Value.ToString());
                        strCandidatIDList = strCandidatIDList + "," + Convert.ToString(CandidateID);
                        ////dt.Rows.Add(CandidateID);
                    }
                }
            }

            objAIMBO.CandidatIDList = strCandidatIDList.Substring(1, strCandidatIDList.Length - 1);
            objAIMBO.AssessorUserID = CommonUtility.GetLoggedOnUserId();
            strReturnValue = (string)objAIMBal.SetAssessorInvoice(objAIMBO);

            if (strReturnValue == "SUCCESS")
            {
                GetData();
                ClearData();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Invoice saved succesfully.');window.location='../Assessor/AssessorInvoiceDetail.aspx';", true);
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Invoice saved succesfully.');", true);
                //lblMessage.Text = "Assessor Invoice saved succesfully.";
                //lblMessage.ForeColor = System.Drawing.Color.Green;
                //lblMessage.Font.Bold = true;

            }
            else if (strReturnValue == "EXIST")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Brief already exists.');", true);
                //lblMessage.Text = "Brief already exists."; //EnrollResources.GetMessage(Message.InsertMessage); // 
                //lblMessage.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                //lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }

    }

    //protected void gvAInvoice_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    gvAInvoice.PageIndex = e.NewPageIndex;
    //    GetData();

    //}
    protected void gvAInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void gvAInvoice_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void gvAInvoice_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void GetData()
    {
        gvAInvoice.DataSource = GetDataSet();
        gvAInvoice.DataBind();
    }
    protected void btnCancel_OnClick(object sender, EventArgs e)
    {
        // ClearData();
        Response.Redirect("AssessorInvoiceDetail.aspx");
    }

    //private void InvoiceFileSave()
    //{
    //    string strPath = string.Empty;
    //    string strFileExt = string.Empty;
    //    string strFileName = string.Empty;
    //    string strFileExt1 = string.Empty;
    //    strFileExt = Path.GetExtension(txtUploadInvoice.PostedFile.FileName);
    //    string strDateYY = System.DateTime.Now.ToShortDateString();
    //    string strDateMM = CommonUtility.ConvertDateToddMMYYY_hhmmss(System.DateTime.Now.ToString());
    //    string strYYYY = strDateYY.Replace("/", "_");
    //    string strYYYYM = strDateMM.Remove(0, 11);
    //    string strYYYYMM = strYYYYM.Replace(":", "_");
    //    string strDateConYYMM = strYYYY + strYYYYMM;
    //    strFileName = txtUploadInvoice.FileName.Replace(Convert.ToString(txtUploadInvoice.FileName), txtInvoiceNo.Text.Trim());
    //    strPath = MapPath(Convert.ToString(ConfigurationManager.AppSettings["AssessorInvoiceDoc"]));
    //    hdnUploadDoc.Value = strPath + strFileName + "_" + strDateConYYMM + strFileExt;
    //    txtUploadInvoice.SaveAs(hdnUploadDoc.Value);
    //}
}